home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / DebugGraphicsPanel.java < prev    next >
Text File  |  1998-06-30  |  11KB  |  320 lines

  1. /*
  2.  * @(#)DebugGraphicsPanel.java    1.4 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.event.*;
  23. import com.sun.java.swing.text.*;
  24. import com.sun.java.accessibility.*;
  25.  
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import java.util.*;
  29.  
  30.  
  31. /**
  32.  * Debug Graphics!
  33.  *
  34.  * @version 1.4 02/02/98
  35.  * @author Jeff Dinkins
  36.  * @author Peter Korn (accessibility support)
  37.  */
  38. public class DebugGraphicsPanel extends JPanel 
  39. {
  40.     // The Frame
  41.     SwingSet swing;
  42.     JPanel components;
  43.  
  44.     JButton button = new JButton("Button");
  45.     JRadioButton radio = new JRadioButton("RadioButton");
  46.     JCheckBox check = new JCheckBox("Checkbox");
  47.     JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
  48.     JScrollBar scrollbar = new JScrollBar(JScrollBar.HORIZONTAL, 50, 20, 0, 100);
  49.  
  50.     JSlider flashSlider = new JSlider(JSlider.HORIZONTAL, 1, 50, 10);
  51.     JCheckBox buttonCheckbox = new JCheckBox("Button");
  52.     JCheckBox radioCheckbox = new JCheckBox("RadioButton");
  53.     JCheckBox checkboxCheckbox = new JCheckBox("Checkbox");
  54.     JCheckBox sliderCheckbox = new JCheckBox("Slider");
  55.     JCheckBox scrollbarCheckbox = new JCheckBox("ScrollBar");
  56.  
  57.     DebugGraphicsListener debugGraphicsListener = new DebugGraphicsListener();
  58.     ChangeListener sliderListener;
  59.  
  60.     public DebugGraphicsPanel(SwingSet swing) {
  61.     this.swing = swing;
  62.     sliderListener = new ChangeListener() {
  63.         public void stateChanged(ChangeEvent e) {
  64.         JSlider s = (JSlider)e.getSource();
  65.         DebugGraphics.setFlashTime(s.getValue());
  66.         }
  67.     };
  68.     flashSlider.addChangeListener(sliderListener);
  69.     
  70.  
  71.     setBorder(swing.emptyBorder5);
  72.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  73.  
  74.     // *************** buttons ****************
  75.     components = SwingSet.createVerticalPanel(true);
  76.     components.setBorder(swing.emptyBorder10);
  77.  
  78.         button.setEnabled(false);
  79.         radio.setEnabled(false);
  80.         radio.setSelected(true);
  81.         check.setEnabled(false);
  82.         check.setSelected(true);
  83.     
  84.     // Add buttons to buttonPanel
  85.     JPanel buttonPanel = swing.createHorizontalPanel(false);
  86.     buttonPanel.setBorder(swing.etchedBorder10);
  87.     buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
  88.     buttonPanel.setAlignmentY(TOP_ALIGNMENT);
  89.  
  90.     JLabel l = (JLabel) components.add(new JLabel("Buttons"));
  91.     l.setFont(swing.boldFont);
  92.     buttonPanel.add(button);
  93.     buttonPanel.add(Box.createRigidArea(swing.hpad20));
  94.     buttonPanel.add(radio);
  95.     buttonPanel.add(Box.createRigidArea(swing.hpad20));
  96.     buttonPanel.add(check);
  97.     components.add(buttonPanel);
  98.     components.add(Box.createRigidArea(swing.vpad20));
  99.  
  100.     // *************** slider ****************
  101.  
  102.         // slider.setEnabled(false);
  103.     slider.setEnabled(false);
  104.     slider.setPaintTicks(true);
  105.     slider.setMinorTickSpacing(10);
  106.     slider.setMajorTickSpacing(40);
  107.     slider.getAccessibleContext().setAccessibleName("Sample slider");
  108.  
  109.     JPanel sliderPanel = new JPanel() {
  110.         public Dimension getMaximumSize() {
  111.         return new Dimension(super.getMaximumSize().width, 60);
  112.         }
  113.     };
  114.     sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.X_AXIS));
  115.     sliderPanel.setBorder(swing.etchedBorder10);
  116.     sliderPanel.setAlignmentX(LEFT_ALIGNMENT);
  117.     sliderPanel.setAlignmentY(TOP_ALIGNMENT);
  118.  
  119.     l = (JLabel) components.add(new JLabel("Slider"));
  120.     l.setFont(swing.boldFont);
  121.     sliderPanel.add(slider);
  122.     components.add(sliderPanel);
  123.     components.add(Box.createRigidArea(swing.vpad20));
  124.  
  125.     // *************** scrollbar ****************
  126.  
  127.         scrollbar.setEnabled(false);
  128.     scrollbar.getAccessibleContext().setAccessibleName("Sample scrollbarlider");
  129.  
  130.     JPanel scrollbarPanel = new JPanel() {
  131.         public Dimension getMaximumSize() {
  132.         return new Dimension(super.getMaximumSize().width, 60);
  133.         }
  134.     };
  135.     scrollbarPanel.setLayout(new BoxLayout(scrollbarPanel, BoxLayout.Y_AXIS));
  136.     scrollbarPanel.setBorder(swing.etchedBorder10);
  137.     scrollbarPanel.setAlignmentX(LEFT_ALIGNMENT);
  138.     scrollbarPanel.setAlignmentY(TOP_ALIGNMENT);
  139.  
  140.     JPanel box = swing.createHorizontalPanel(false);
  141.     box.add(Box.createRigidArea(swing.hpad5));
  142.     box.add(scrollbar);
  143.     box.add(Box.createRigidArea(swing.hpad5));
  144.  
  145.     l = (JLabel) components.add(new JLabel("ScrollBar"));
  146.     l.setFont(swing.boldFont);
  147.     scrollbarPanel.add(Box.createRigidArea(swing.vpad5));
  148.     scrollbarPanel.add(box);
  149.     scrollbarPanel.add(Box.createRigidArea(swing.vpad5));
  150.     components.add(scrollbarPanel);
  151.     components.add(Box.createRigidArea(swing.vpad20));
  152.  
  153.     l = (JLabel) components.add(new JLabel("Note: the above components are intentionally disabled."));
  154.     components.add(Box.createRigidArea(swing.vpad5));
  155.     l = (JLabel) components.add(new JLabel("Choose a component checkbox at right, then click on"));
  156.     l = (JLabel) components.add(new JLabel("the \"Repaint\" button to see debug graphics at work."));
  157.  
  158.  
  159.     // ***** fill out the rest of the panel
  160.     components.add(Box.createGlue());
  161.  
  162.     // *************** Create the debug graphics controls ****************
  163.     JPanel controls = new JPanel() {
  164.         public Dimension getMaximumSize() {
  165.         return new Dimension(300, super.getMaximumSize().height);
  166.         }
  167.     };
  168.     controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
  169.  
  170.     JPanel debugControls = swing.createHorizontalPanel(true);
  171.     debugControls.setAlignmentY(TOP_ALIGNMENT);
  172.     debugControls.setAlignmentX(LEFT_ALIGNMENT);
  173.  
  174.     JPanel leftColumn = swing.createVerticalPanel(false);
  175.     leftColumn.setAlignmentX(LEFT_ALIGNMENT);
  176.     leftColumn.setAlignmentY(TOP_ALIGNMENT);
  177.  
  178.     debugControls.add(leftColumn);
  179.     debugControls.add(Box.createRigidArea(swing.hpad20));
  180.     debugControls.add(Box.createRigidArea(swing.hpad20));
  181.  
  182.     controls.add(debugControls);
  183.  
  184.     // Display Options
  185.     l = new JLabel("Use Debug Graphics On:");
  186.     leftColumn.add(l);
  187.     l.setFont(swing.boldFont);
  188.  
  189.      buttonCheckbox.addItemListener(debugGraphicsListener);
  190.     buttonCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  191.      leftColumn.add(buttonCheckbox);
  192.  
  193.      radioCheckbox.addItemListener(debugGraphicsListener);
  194.     radioCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  195.      leftColumn.add(radioCheckbox);
  196.  
  197.      checkboxCheckbox.addItemListener(debugGraphicsListener);
  198.     checkboxCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  199.      leftColumn.add(checkboxCheckbox);
  200.  
  201.      sliderCheckbox.addItemListener(debugGraphicsListener);
  202.     sliderCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  203.      leftColumn.add(sliderCheckbox);
  204.  
  205.      scrollbarCheckbox.addItemListener(debugGraphicsListener);
  206.     scrollbarCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  207.      leftColumn.add(scrollbarCheckbox);
  208.  
  209.     // debug flashTime
  210.     leftColumn.add(Box.createRigidArea(swing.vpad40));
  211.     l = new JLabel("Debug Flash Interval:");
  212.     l.setFont(swing.boldFont);
  213.     leftColumn.add(l);
  214.     leftColumn.add(flashSlider);
  215.     flashSlider.setMaximumSize(new Dimension(150, 60));
  216.     flashSlider.setPaintTicks(true);
  217.     flashSlider.setMinorTickSpacing(5);
  218.     flashSlider.setMajorTickSpacing(20);
  219.     flashSlider.setToolTipText("Sets the sleep time between graphics operations, from 1 to 50 milliseconds");
  220.     flashSlider.getAccessibleContext().setAccessibleName("Debug Flash Interval");
  221.  
  222.     // repaint button
  223.     leftColumn.add(Box.createRigidArea(swing.vpad40));
  224.      JButton repaintButton = new JButton("Repaint");
  225.     repaintButton.setToolTipText("Causes the selected components to be repainted using DebugGraphics.");
  226.      repaintButton.addActionListener(debugGraphicsListener);
  227.      leftColumn.add(repaintButton);
  228.      leftColumn.add(Box.createGlue());
  229.  
  230.     add(components);
  231.     add(Box.createRigidArea(swing.hpad10));
  232.      add(controls);
  233.     }
  234.  
  235.     public void resetAll() {
  236.     scrollbarCheckbox.setSelected(false);
  237.     buttonCheckbox.setSelected(false);
  238.     radioCheckbox.setSelected(false);
  239.     sliderCheckbox.setSelected(false);
  240.     checkboxCheckbox.setSelected(false);
  241.     }
  242.  
  243.     
  244.     class DebugGraphicsListener implements ItemListener, ActionListener {
  245.         boolean repaintButton = false;
  246.         boolean repaintRadio = false;
  247.         boolean repaintCheck = false;
  248.         boolean repaintSlider = false;
  249.         boolean repaintScrollBar = false;
  250.  
  251.         public void actionPerformed(ActionEvent e) {
  252.         if(e.getSource() instanceof JButton) {
  253.             if(repaintButton) {
  254.             button.repaint();
  255.             }
  256.             if(repaintRadio) {
  257.             radio.repaint();
  258.             }
  259.             if(repaintCheck) {
  260.             check.repaint();
  261.             }
  262.             if(repaintSlider) {
  263.             slider.repaint();
  264.             }
  265.             if(repaintScrollBar) {
  266.             scrollbar.repaint();
  267.             }
  268.         }
  269.         }
  270.  
  271.         public void itemStateChanged(ItemEvent e) {
  272.         AbstractButton b = (AbstractButton) e.getSource();
  273.         String label = b.getText();
  274.         if(label.equals("Button")) {
  275.             if(b.isSelected()) {
  276.             button.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  277.             repaintButton = true;
  278.             } else {
  279.             button.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  280.             repaintButton = false;
  281.             }
  282.         } else if(label.equals("RadioButton")) {
  283.             if(b.isSelected()) {
  284.             radio.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  285.             repaintRadio = true;
  286.             } else {
  287.             radio.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  288.             repaintRadio = false;
  289.             }
  290.         } else if(label.equals("Checkbox")) {
  291.             if(b.isSelected()) {
  292.             check.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  293.             repaintCheck = true;
  294.             } else {
  295.             check.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  296.             repaintCheck = false;
  297.             }
  298.         } else if(label.equals("Slider")) {
  299.             if(b.isSelected()) {
  300.             slider.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  301.             repaintSlider = true;
  302.             } else {
  303.             slider.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  304.             repaintSlider = false;
  305.             }
  306.         } else if(label.equals("ScrollBar")) {
  307.             if(b.isSelected()) {
  308.             scrollbar.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  309.             repaintScrollBar = true;
  310.             } else {
  311.             scrollbar.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  312.             repaintScrollBar = false;
  313.             }
  314.         } else {
  315.         }
  316.     }
  317.     }
  318.  
  319. }
  320.